
public class Leetcode2047 {
    public static void main(String[] args) {
        System.out.println(countValidWords("cat and  dog"));
    }

    public static int countValidWords(String sentence) {
        String regex = "([a-z]*|([a-z]+-[a-z]+))[!.,]?";
        sentence = sentence.trim();
        String[] sens = sentence.split(" +");
        int res = 0;
        for (String s : sens) {
            if (s.matches(regex)) {
                res++;
            }
        }
        return res;
    }
}
